/**
 *  DBEXEL is a Database Backed & Web-Based Worksheet and chart management program. 
 *  It has been influenced by Excel.
 *  For questions or suggestions please contact the developper at ( Development@Gandomi.com )
 *  Copyright (C) 2011 Baubak Gandomi   
 *
 *	This file is part of the application DBEXEL
 *
 *   DBEXEL is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   DBEXEL is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package dbexel.presentation.actions;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

import dbexel.model.dao.WorkSheetDao;
import dbexel.model.dao.WorkSheetDaoImpl;
import dbexel.model.mapping.AttributeValue;
import dbexel.model.mapping.WorkSheet;
import dbexel.model.mapping.WorkSheet_Attribute;
import dbexel.model.mapping.WorkSheet_Entry;

public class InitWorkSheet extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String workSheetName;
	private String sentence;
	private List<String> attributeValues = new ArrayList<String>();
	private List<String> attributeNames = new ArrayList<String>();
	private List<String> cellActions = new ArrayList<String>();
	
	private Long workSheet_Id;
	private WorkSheetDao workSheetDao;

	public String getSentence() {
		return sentence;
	}

	public void setSentence(String sentence) {
		this.sentence = sentence;
	}

	public String getWorkSheetName() {
		return workSheetName;
	}

	public void setWorkSheetName(String workSheetName) {
		this.workSheetName = workSheetName;
	}

	// This method divides the given sentence into an array based on the spaces
	// in between
	protected List<String> slashSentence() {

		return Arrays.asList(getSentence().split(" "));
	}

	// This is the action method
	public String prepareNewWorkSheet() {
		// slash the sentence
		setAttributeValues(slashSentence());
		
		
		return ActionSupport.INPUT;

	}

	/**
	 * @return the attributeName
	 */
	public List<String> getAttributeNames() {
		return attributeNames;
	}

	/**
	 * @param in_attributeName
	 *            the attributeName to set
	 */
	public void setAttributeNames(List<String> in_attributeName) {
		attributeNames = in_attributeName;
	}

	/**
	 * @return the cellAction
	 */
	public List<String> getCellActions() {
		return cellActions;
	}

	/**
	 * @param in_cellAction
	 *            the cellAction to set
	 */
	public void setCellActions(List<String> in_cellActions) {
		cellActions = in_cellActions;
	}

	/**
	 * @param attributeValues
	 *            the attributeValues to set
	 */
	public void setAttributeValues(List<String> attributeValues) {
		this.attributeValues = attributeValues;
	}

	/**
	 * @return the attributeValues
	 */
	public List<String> getAttributeValues() {
		return attributeValues;
	}

	/**
	 * This method applies the cell actions to the attribue value and names
	 */
	public String performCellActions() {
		CellOptionActions lt_currentAction;
		
		//create the worksheet
		WorkSheet l_ws = new WorkSheet(getWorkSheetName());
		l_ws.addEntry(new WorkSheet_Entry());		
		
		WorkSheet_Attribute lt_attr = null;
		AttributeValue lt_av = null;
		
		for (int i = 0; i < getCellActions().size(); i++) {
			// Fetch the action of the current index
			lt_currentAction = CellOptionActions.valueOf(getCellActions().get(i));

			switch (lt_currentAction) {
			case Delete:
				//if Delete we do not do anything
				break;

			default:
				//Otherwise we add the attribute information to the temporary attribute info
/*				lt_attr = new WorkSheet_Attribute(getAttributeNames().get(i));
				l_ws.attachAttribute(lt_attr);
				lt_av = new AttributeValue(lt_attr, getAttributeValues().get(i));
				l_avDao.createAttributeValue(lt_av);
				l_ws.getEntries().get(0).attachAttributeValue(lt_av);
				l_wsDao.updateWorkSheet(l_ws);
*/				
				//Original
				//l_newAttributeNames.add(getAttributeNames().get(i));
				//l_newAttributeValues.add(getAttributeValues().get(i));
				
				//Get attribute
				lt_attr = new WorkSheet_Attribute(getAttributeNames().get(i));
				
				//Attach attribute to the worksheet
				l_ws.attachAttribute(lt_attr);
				
				//Create attribute value
				lt_av = new AttributeValue(lt_attr, getAttributeValues().get(i));
				
				//attach attribute value to entry
				l_ws.getEntries().get(0).attachAttributeValue(lt_av);
				
				break;
			}

		}
		
		/* Original
		setAttributeNames(l_newAttributeNames);
		setAttributeValues(l_newAttributeValues);
		*/
		
		//Store the worksheet
		
		//WorkSheetDao l_wsDao = new WorkSheetDaoImpl();
		getWorkSheetDao().createWorkSheet(l_ws);
		
		setWorkSheet_Id(l_ws.getWs_Id());
		
		return ActionSupport.SUCCESS;
	}

	//returns the list of possible actions
	public List<String> getCellActionOptions() {
		return CellOptionActions.stringValues();
	}

	/**
	 * @param workSheet_Id the workSheet_Id to set
	 */
	public void setWorkSheet_Id(Long workSheet_Id) {
		this.workSheet_Id = workSheet_Id;
	}

	/**
	 * @return the workSheet_Id
	 */
	public Long getWorkSheet_Id() {
		return workSheet_Id;
	}

	/**
	 * @return the workSheetDao
	 */
	public WorkSheetDao getWorkSheetDao() {
		if (workSheetDao == null)
			setWorkSheetDao(new WorkSheetDaoImpl());

		return workSheetDao;
	}

	/**
	 * @param workSheetDao the workSheetDao to set
	 */
	public void setWorkSheetDao(WorkSheetDao workSheetDao) {
		this.workSheetDao = workSheetDao;
	}

}
